/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import Entity.Customer;
import Entity.TicketsSummary;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Cyrax
 */
@Stateful
public class TicketsSumSessionBean implements TicketsSumSessionBeanLocal {

    @PersistenceContext(unitName = "EasyTickets-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    @Override
    public List<TicketsSummary> listAllTicketsSum() {
        List<TicketsSummary> tks;
        tks = em.createNamedQuery("TicketsSummary.findAll").getResultList();
        return tks;
    }

    @Override
    public List<TicketsSummary> searchTicketsSummary(String email) {
        List<TicketsSummary> ts = null;

        ts = em.createQuery("SELECT t FROM TicketsSummary t WHERE t.email like :email").setParameter("email", "%" + email + "%").getResultList();
        System.out.println("userName : " + email);


        return ts;
    }

    @Override
    public Boolean addCart(TicketsSummary cart) {
        try {
            em.persist(cart);
            return Boolean.TRUE;
        }
        catch(Exception ex){
            return Boolean.FALSE;
        }
    }

    @Override
    public TicketsSummary viewCartByCus(Customer c){
        TicketsSummary ts;
        ts = (TicketsSummary) em.createQuery("SELECT t FROM TicketsSummary t WHERE t.customer = :c").setParameter("c", c).getSingleResult();
        return ts;
    }
}
